#pragma once

#include <iostream>
#include <vector>
#include <queue>
#include <pthread.h>
using namespace ThreadNS;

#define MAXTHREAD 5;

template<class T>
class PthreadPool
{
public:

    PthreadPool(const int& _cap = MAXTHREAD)
    :_threads(_cap)
    ,_capethread(_cap)
    ,_queue(_cap) // 先把队列的大小也设置为线程的个数
    {
        pthread_mutex_init(&_mutex);
        pthread_cond_init(&_condition);      

        //开始创建线程但是不启动
        for(int i = 1 ; i <= MAXTHREAD; i++)
        {
            Thread thread();
            _threads.push_back(&thread);
        }
    }
     
    ~PthreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_condition);
    }


private:   
   std::vector<Thread*> _threads;  // 线程池, 阻塞队列
   std::queue<T> _queue;
   size_t _capeThread;

   // 这是普通的队列，用条件变量和互斥一把锁保证只有一个线程与队列交换
   pthread_cond_t  _condition;
   pthread_mutex_t _mutex;
};